﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Card.Web.Member
{
    public partial class Password : MemberBase
    {
        #region 属性

        #endregion

        
        #region 事件
        protected override void OnInit(EventArgs e)
        {
            this.btnSave.Click += new EventHandler(btnSave_Click);
            base.OnInit(e);
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            if (this.DoCheck())
            {
                Entity.Users user = BLL.UsersManager.GetEntity(this.Passport.UserID, this.Passport);
                if (user != null)
                {
                    if (Security.Utility.ValidatePassword(user.Password, this.txtPW.Text.Trim()))
                    {
                        user.Password = Security.Utility.EncryptPassWord(this.txtNewPW.Text.Trim());
                        if (BLL.UsersManager.Update(user, this.Passport))
                        {
                            this.MessageBox("修改成功！");
                        }
                    }
                    else
                    {
                        this.MessageBox("原始密码输入错误！");
                    }
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        #endregion

        #region 方法
        private bool DoCheck()
        {
            if (string.IsNullOrEmpty(this.txtNewPW.Text) || this.txtNewPW.Text.Length < 6)
            {
                this.MessageBox("密码不能为空，且长度不少于6个字符！");
                return false;
            }

            if (string.Compare(this.txtNewPW.Text.Trim(), this.txtReNewPW.Text.Trim()) != 0)
            {
                this.MessageBox("两次输入的密码不一致！");
                return false;
            }

            return true;
        }
        #endregion
    }
}
